<?php
/**
 * 
 * 找回密码
 * @author Administrator
 *
 */

class Api_retpassword_mdl extends CI_Model
{
	
	
	public function __construct()
	{
		parent::__construct();
	}
	
	
	public function outputdata($data)
	{
		if ( !empty($data['EMAIL']) ) {
			
			$user = $this->_checkUser( $data['EMAIL'] );
			if ( !empty($user) ) {
				
				$ret = array();
				$ret = $this->_checkret( $user['uid'] );
				$retcode = $this->_getActiveCode();
				$this->load->model( 'email_mdl', 'email' );
				
				if (empty( $ret )) {
					
					//重新发送验证码		
					$retinfo = array( 'retcode'=>$retcode, 'createtime'=>time(), 'uid'=>$user['uid']);
					$result = $this->_addRetInfo($retinfo);		
					
				} else {
					
					$retinfo = array( 'retcode'=>$retcode, 'createtime'=>time(), 'uid'=>$user['uid']);
					$result = $this->_updateRetInfo($ret['id'], $retinfo);
					
				}
				
				//发邮件
				$this->email->sendEmail(  '',$data['EMAIL'],$retcode  );
				//添加或者修改找回信息
				$outputdata['TAG'] = $data['TAG'];
				$outputdata['STATUS'] = 0;
				$outputdata['ERRORMESSAGE'] = $retcode;				
				
			} else {
				
				//用户不存在
				$outputdata['TAG'] = $data['TAG'];
				$outputdata['STATUS'] = 1;
				$outputdata['ERRORMESSAGE'] = 'NO USER';
							
			}
		} else {
			
			//邮箱为空
			$outputdata['TAG'] = $data['TAG'];
			$outputdata['STATUS'] = 1;
			$outputdata['ERRORMESSAGE'] = 'INFO ERROR';
			
		}
		
		header('Content-Type:text/html; charset=utf-8', false);
		echo json_encode($outputdata);
	}
	
	/**
	 * 
	 * 验证用户是否存在
	 * @param String $email
	 */
	private function _checkUser( $email )
	{
		$user = array();
		$user = $this->db->select( 'uid,txusername,txemail,active,lastlogin,txpassword,txgoldnum' )
						->where( 'txemail', $email )
						->get( 'cj_users' )
						->row_array();
		return $user;
	}
	
	/**
	 * 
	 * 检查找回密码信息
	 */
	private function _checkret( $uid )
	{
		
		$ret = array();
		$ret = $this->db->select( 'id, retcode, uid, createtime, used' )
					->from( 'cj_retpassword' )
					->where( 'uid', $uid )
					->where( 'used', 0 )
					->get()
					->row_array();
					
		return $ret;
		
	}
	
	/**
	 * 修改找回密码信息
	 */
	private function _updateRetInfo( $id, $retinfo )
	{
		
		$this->db->where('id', $id );
		$this->db->update('cj_retpassword', $retinfo);
		
		return $this->db->affected_rows();
		
	}
	
	/**
	 * 
	 * 添加信息
	 * @param Array $data
	 */
	private function _addRetInfo( $data )
	{
		
		$this->db->insert('cj_retpassword',$data);
		return ($this->db->affected_rows() > 0) ? $this->db->insert_id() : FALSE;
		
	}
	
	
	/**
	 * 产生激活码
	 */
	
	private function _getActiveCode()
	{
		$str = '1234567890abcdefghijklmnopqrsduvwxyz';
		$len = strlen($str);
		$randnum = '';
		
		for ($i = 0; $i < 6; $i++) { 
			$start = rand(1,strlen($str) - 1); 
			$randnum .= substr($str,$start,1); 
		} 
		
		return $randnum;
		
	}
	
	/**
	 * 
	 * 发送邮件
	 * @param String $form
	 * @param Sting $to
	 * @param String $body
	 */
	private function _email( $form,$to,$body )
	{
		
			$this->load->library('phpmailer');
			try {
				$this->phpmailer->IsSMTP();                           // tell the class to use SMTP
				$this->phpmailer->SMTPAuth   = true;                  // enable SMTP authentication
				$this->phpmailer->Port       = 25;                    // set the SMTP server port
				$this->phpmailer->Host       = "smtp.163.com"; // SMTP server
				$this->phpmailer->Username   = "lottery2013888@163.com";     // SMTP server username
				$this->phpmailer->Password   = "lottery2013";            // SMTP server password
			
				$this->phpmailer->IsSendmail();  // tell the class to use Sendmail
			
				$this->phpmailer->AddReplyTo($to,"First Last");
			
				$this->phpmailer->From       = "lottery2013888@163.com";
				$this->phpmailer->FromName   = "lottery";
	
				$this->phpmailer->AddAddress($to);
			
				$this->phpmailer->Subject  = "First PHPMailer Message";
			
				$this->phpmailer->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
				$this->phpmailer->WordWrap   = 80; // set word wrap
			
				$this->phpmailer->MsgHTML($body);
			
				$this->phpmailer->IsHTML(true); // send as HTML
			
				$this->phpmailer->Send();
				
				/*-------------------------*/
				$fp = fopen('email.txt','a+');
				fwrite($fp,$to.': '.date("Y-m-d H:i:s")."\r\n");
				fclose($fp);
				/*-------------------------*/
				return true;
			} catch ( phpmailerException $e ){
				
				return false;
				
			}
		

	}
	
}